package com.boco.modules.fdoc.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.boco.common.enums.ApiStatusEnum;
import com.boco.common.json.BaseJsonVo;
import com.boco.common.json.MessageResult;
import com.boco.common.utils.JsonUtils;
import com.boco.common.utils.MatcherUtiles;
import com.boco.common.web.BaseController;
import com.boco.modules.fdoc.model.UserRelationsEntity;
import com.boco.modules.fdoc.service.CommonService;
import com.boco.modules.fdoc.service.UserRelationsService;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wordnik.swagger.annotations.ApiOperation;

/**
 * 用户家庭成员 api
 * 
 * @author sufj
 *
 */
@RestController
@RequestMapping(value = "/userRelations", produces = "text/json;charset=UTF-8")
public class UserRelationslApi extends BaseController {
	
	@Resource
	CommonService commonService;
	
	@Resource
	UserRelationsService userRelationService;

	@RequestMapping(value = "/addMember", method = RequestMethod.POST)
	@ApiOperation(value = "添加家庭成员", notes = "{'uid':'用户ID','name':'姓名','idCard':'身份证','tel':'电话','sex':'性别','age':'年龄','relation':'与当前用户关系','area':'所属区域','img':'证件照URL','contactPerson':'默认联系人','patientCardNum':'就诊卡号'}", response = MessageResult.class)
	public String addMember(@RequestBody String memberBody) {
		UserRelationsEntity en = JsonUtils.getObject(memberBody, UserRelationsEntity.class);
		//验证身份证号是否合法
		if (!MatcherUtiles.idCardMach(en.getIdCard())) {
			return JsonUtils.getJson(BaseJsonVo.empty(ApiStatusEnum.IDCARD_OUTLAW.getKey(), ApiStatusEnum.IDCARD_OUTLAW.getValue()));
		}
		//验证手机号是否合法
		if (!MatcherUtiles.mobileMach(en.getTel())) {
			return JsonUtils.getJson(BaseJsonVo.empty(ApiStatusEnum.MOBILE_OUTLAW.getKey(), ApiStatusEnum.MOBILE_OUTLAW.getValue()));
		}
		int newId = userRelationService.insert(en);
		en.setId(newId);
		return JsonUtils.getJson(BaseJsonVo.success(en));
	}

	@RequestMapping(value = "/updateMember", method = RequestMethod.POST)
	@ApiOperation(value = "更新家庭成员信息", notes = "{'id':'id','uid':'用户ID','name':'姓名','idCard':'身份证',"
	+"'tel':'电话','sex':'性别','age':'年龄','relation':'与当前用户关系','area':'所属区域','img':'证件照URL',"
	+"'contactPerson':'默认联系人','patientCardNum':'就诊卡号'}", response = MessageResult.class)
	public String updateMember(@RequestBody String memberBody) {
		UserRelationsEntity en = JsonUtils.getObject(memberBody, UserRelationsEntity.class);
		//验证身份证号是否合法
		if (!MatcherUtiles.idCardMach(en.getIdCard())) {
			return JsonUtils.getJson(BaseJsonVo.empty(ApiStatusEnum.IDCARD_OUTLAW.getKey(), ApiStatusEnum.IDCARD_OUTLAW.getValue()));
		}
		//验证手机号是否合法
		if (!MatcherUtiles.mobileMach(en.getTel())) {
			return JsonUtils.getJson(BaseJsonVo.empty(ApiStatusEnum.MOBILE_OUTLAW.getKey(), ApiStatusEnum.MOBILE_OUTLAW.getValue()));
		}
		userRelationService.update(en);
		return JsonUtils.getJson(BaseJsonVo.success(en));
	}

	@RequestMapping(value = "/deleteSingleMember", method = RequestMethod.POST)
	@ApiOperation(value = "删除单个家庭成员", notes = "{'id':'id','uid':'uid'}", response = BaseJsonVo.class)
	public String deleteSingleMember(@RequestBody String memberBody) {
		UserRelationsEntity en = JsonUtils.getObject(memberBody, UserRelationsEntity.class);
		userRelationService.delete(en);
		return JsonUtils.getJson(BaseJsonVo.success(null));
	}

	@RequestMapping(value = "/deleteMultiMember", method = RequestMethod.POST)
	@ApiOperation(value = "批量删除家庭成员", notes = "{'uid':'用户id','data':[{'id':'家庭成员id'},{'id':'家庭成员id'}....]}", 
		response = BaseJsonVo.class)
	public String deleteMultiMember(@RequestBody String memberBody) {

		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(Feature.ALLOW_SINGLE_QUOTES,true);
		mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS,true);
		try {
			HashMap jsonMap = mapper.readValue(memberBody,HashMap.class);
			String uid = String.valueOf(jsonMap.get("uid"));
			ArrayList<HashMap<String,String>> list = (ArrayList) jsonMap.get("data");
			for (HashMap<String, String> hashMap : list) {
				UserRelationsEntity en = new UserRelationsEntity();
				en.setUid(uid);
				en.setId(Integer.parseInt(hashMap.get("id")));
				userRelationService.delete(en);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return JsonUtils.getJson(BaseJsonVo.success(null));
	}

	@RequestMapping(value = "/getMemberList", method = RequestMethod.GET)
	@ApiOperation(value = "获取所有家庭成员信息", notes = "'uid':'uid'", response = BaseJsonVo.class)
	public String getMemberList(@RequestParam String uid) {
		List<UserRelationsEntity> list = userRelationService.getByUid(uid);
		return JsonUtils.getJson(BaseJsonVo.success(list));
	}

}
